package com.example.floristshop;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.floristshop.adapters.RecordAdapter;
import com.example.floristshop.database.Record;

import org.litepal.LitePal;

import java.util.Collections;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class RecordFragment extends Fragment {

    private LinearLayout search_button, add_button;
    private ListView listView;

    RecordAdapter adapter;

    List<Record> recordList;

    public RecordFragment() {
        // Required empty public constructor
    }

    public static RecordFragment newInstance(String param1, String param2) {
        RecordFragment fragment = new RecordFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // init data
        recordList = LitePal.order("timestamp").find(Record.class);
        Collections.reverse(recordList);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // get view
        View rootView = inflater.inflate(R.layout.fragment_record, container, false);
        search_button = (LinearLayout)rootView.findViewById(R.id.record_search_button);
        add_button = (LinearLayout)rootView.findViewById(R.id.record_add_button);
        listView = (ListView) rootView.findViewById(R.id.record_listview);

        // set view
        adapter = new RecordAdapter(getActivity(), R.layout.record_item, recordList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Record record = recordList.get(position);
                // pass data && jump
                Intent intent = new Intent(getContext(), RecordInfoActivity.class);
                intent.putExtra("record_time", record.getTimestamp());
                startActivityForResult(intent, 1);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Record record = recordList.get(position);
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LitePal.deleteAll(Record.class, "timestamp==?", record.getTimestamp()+"");
                        recordList.remove(position);
                        RecordAdapter new_adapter = new RecordAdapter(getActivity(), R.layout.record_item, recordList);
                        listView.setAdapter(new_adapter);
                    }
                });
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.setTitle("提示");
                builder.setMessage("是否删除该条记录？");
                builder.show();
                return true;
            }
        });

        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchRecordActivity.class);
                startActivity(intent);
            }
        });
        add_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddRecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });

        return rootView;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode){
            case 1:
                if(resultCode==RESULT_OK){
                    recordList = LitePal.order("timestamp").find(Record.class);
                    Collections.reverse(recordList);
                    RecordAdapter new_adapter = new RecordAdapter(getActivity(), R.layout.record_item, recordList);
                    listView.setAdapter(new_adapter);
                }
                break;
            default:
        }
    }
}